//
//  TypeViewCollectionCell.m
//  TourPlay
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "TypeViewCollectionCell.h"
#import "ContentMod.h"
#import "MicroTableViewCell.h"
#import "BigTableViewCell.h"
#import "MJRefresh.h"


@interface TypeViewCollectionCell () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIView *typeBtnView;

@property (nonatomic, retain) MBProgressHUD *hub; // 加载转圈
@property (nonatomic, assign) BOOL isHidden;
@property (nonatomic, assign) CGFloat scrollDidValue;
@property (nonatomic, assign) BOOL isUp; // 是否上拉

@property (nonatomic, assign) NSInteger pIndex; // 上拉加载页数

@property (nonatomic, retain) UITableView *smallTableV;
@property (nonatomic, retain) NSMutableArray *contentArr;
@property (nonatomic, retain) NSMutableArray *footerHiddenArr; // 记录刷新


@property (nonatomic, assign) BOOL isHiddenMicroCell; // 切换cell


@property (nonatomic, retain) UIView *backView;


@property (nonatomic, retain) NoContentView *noContentV;
@end


@implementation TypeViewCollectionCell
- (void)dealloc
{
    [_typeBtnView release];
    [_hub release];
    [_smallTableV release];
    [_contentArr release];
    [_noContentV release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    // 初始化数组
    self.contentArr = [NSMutableArray array];
    self.footerHiddenArr = [NSMutableArray array];
    
    self.typeBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
//    self.typeBtnView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    self.typeBtnView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.typeBtnView];
    [_typeBtnView release];
    
//    for (NSInteger i = 0; i < 3; i++) {
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
//        button.frame = CGRectMake((SCREEN_WIDTH - 35) / 3.f * i + 10, 8, (SCREEN_WIDTH - 35 - 10) / 3.f - 10, 28);
//        button.layer.cornerRadius = 4.f;
//        button.tag = 1000 + i;
//        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.typeBtnView addSubview:button];
//    }
    
    // 初始化tableView
    self.smallTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64 - 10) style:UITableViewStylePlain];
    self.smallTableV.delegate = self;
    self.smallTableV.dataSource = self;
    // 取消分割线
    self.smallTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.smallTableV.backgroundColor = [UIColor clearColor];
    [self.smallTableV registerClass:[MicroTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.smallTableV registerClass:[BigTableViewCell class] forCellReuseIdentifier:@"Bigcell"];
    [self.contentView addSubview:self.smallTableV];
    [_smallTableV release];
    
    
    self.backView = [[UIView alloc] initWithFrame:self.smallTableV.frame];
    self.backView.backgroundColor = self.typeBtnView.backgroundColor;
    [self.contentView addSubview:self.backView];
    [_backView release];
    

    self.isHidden = NO;
    self.scrollDidValue = 0.f;
    
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
    // 给三个button添加夜间模式
    [self addColorChangedBlock:^{
        
        for (NSInteger i = 0; i < 3; i++) {
            
            UIButton *button = (UIButton *)[self.typeBtnView viewWithTag:1000 + i];
            
            button.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
            button.normalBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        }
    }];
    
    
    self.noContentV.backgroundColor = [UIColor clearColor];
    
    self.isHiddenMicroCell = YES;
//    self.isUp = NO;
//    self.pIndex = 0;
}
- (void)buttonAction:(UIButton *)sender
{
    NSLog(@"%ld", sender.tag);
    if (1001 == sender.tag) {
        NSLog(@"全部分类");
    } else if (1002 == sender.tag) {
        NSLog(@"时间分类");
    } else {
        NSLog(@"最热分类");
    }
}
#pragma mark - cell切换
- (void)hiddenMicroCellWithIsHidden:(BOOL)isHidden
{
    self.isHiddenMicroCell = isHidden;
    [self.smallTableV reloadData];
}
#pragma mark - 上拉下拉刷新
-(void)addHeaderRefresh
{
    self.smallTableV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUp = NO;
         self.pIndex = 0;
        [self getContent];
    }];
}
-(void)addFooterRefresh
{   // 当上拉时候触发该方法
    self.smallTableV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUp = YES;
        self.pIndex++;
        [self getContent];
    }];
}
#pragma mark - scrollView滑动相关
#if 0
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = self.smallTableV.contentOffset.y - self.scrollDidValue;
//    NSLog(@"----%f", self.scrollDidValue);
    if (offset >= 200) {
        NSLog(@"000000000000000");
        self.isHidden = !self.isHidden;
        
        if (self.isHidden) {
//            [UIView animateWithDuration:0.5f animations:^{
            
                self.typeBtnView.frame = CGRectMake(0, -44, SCREEN_WIDTH, 44);
//            }];
        } else {
//            [UIView animateWithDuration:0.5f animations:^{
            
                self.typeBtnView.frame = CGRectMake(0, 44, SCREEN_WIDTH, 44);
//            }];
        }
        self.scrollDidValue = self.smallTableV.contentOffset.y;
    }
    

}
#endif
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isHiddenMicroCell) {
        return (SCREEN_HEIGHT - 44 - 49 - 64) / 1.15f;
    }
    return (SCREEN_HEIGHT - 50 - 49 - 64) / 3.f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (0 == self.contentArr.count) {
        self.noContentV.hidden = NO;
    } else {
        self.noContentV.hidden = YES;
    }
    return self.contentArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    static NSString *cellBig = @"Bigcell";
    
     ContentMod *contMod = [self.contentArr objectAtIndex:indexPath.row];
    
    
    if (self.isHiddenMicroCell) {
        MicroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellBig];
        cell.contentMod = contMod;
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    } else {
        MicroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentMod = contMod;
        return cell;
    }
   
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 选中时有置灰效果, 离开时消失选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.delegate navigationViewWithIsHidden:YES];
    
    ContentMod *contMod = [self.contentArr objectAtIndex:indexPath.row];
    [self.delegate goToActivityWithId:contMod.activityId activityTitle:contMod.title];
}
- (UIView *)noContentV
{
    if (nil == _noContentV) {
        self.noContentV = [[NoContentView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
        self.noContentV.hidden = YES;
        [self.contentView addSubview:self.noContentV];
        [_noContentV release];
    }
    return _noContentV;
}
- (void)setTitleMod:(TypeTitleModel *)titleMod
{
    if (_titleMod != titleMod) {
        [_titleMod release];
        _titleMod = [titleMod retain];
    }

    
    [self.contentView bringSubviewToFront:self.backView];
    
    
    // 防止菊花重用
    // 让菊花停止旋转
    [self.hub hide:YES];
    
    // 让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"努力加载中...";
    
    

    // 初始化
    self.isUp = NO;
    self.pIndex = 0;
    
    // 让table移动到顶部
    [self.smallTableV setContentOffset:CGPointMake(0,0) animated:NO];
    [self getContent];
    
}
#pragma mark - 获取内容数据
- (void)getContent
{
    NSString *cityid = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityid"];
    NSString *url = [NSString stringWithFormat:@"http://wanapi.damai.cn/projlistnew.json?cateid=%@&cityId=%@&etime=&order=pri&pindex=%ld&psize=20&source=10344&stime=&version=20001", self.titleMod.titleId, cityid, self.pIndex];
    
    [AppTools getAFNet:url block:^(id result) {
 
        // 如果是下拉
        if (!self.isUp) {
            [self.contentArr removeAllObjects];
        }
        
        NSMutableDictionary *bigDic = result;
        
        [self.footerHiddenArr removeAllObjects];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"data"]) {
            ContentMod *contentMod = [ContentMod modelWithDictionary:dic];
            [self.contentArr addObject:contentMod];
            [self.footerHiddenArr addObject:contentMod];
        }
        [self.contentView bringSubviewToFront:self.smallTableV];
        [self.smallTableV reloadData];
        //结束下拉上拉刷新
        [self.smallTableV.header endRefreshing];
        [self.smallTableV.footer endRefreshing];
        
        
        if (self.footerHiddenArr.count < 20) {
            // 变为没有更多数据的状态
//            [self.smallTableV.footer endRefreshingWithNoMoreData];
            // 没有数据隐藏上拉
            self.smallTableV.footer.hidden = YES;
        }
 

        // 让菊花停止旋转
        [self.hub hide:YES];
    } errorBlock:^(NSError *error) {
        
        //结束下拉上拉刷新
        [self.smallTableV.header endRefreshing];
        [self.smallTableV.footer endRefreshing];
        // 让菊花停止旋转
        [self.hub hide:YES];
    }];
}

@end
